#include <sys/types.h>
#include <sys/stat.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

int main(void)
{
    int fd = open("/dev/globalfifo_epoll", O_RDONLY);

    if (-1 != fd)
    {
        struct epoll_event ev_globalfifo;
        int err;

        int epfd = epoll_create(1);
        if (epfd < 0)
        {
            perror("epoll_create(1)");
            return -1;
        }

        bzero(&ev_globalfifo, sizeof(struct epoll_event));
        ev_globalfifo.data.fd = fd;
        ev_globalfifo.events = EPOLLIN | EPOLLOUT | EPOLLET;
        err = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev_globalfifo);
        if (err < 0)
        {
            perror("epoll_ctl()");
            return -1;
        }

        while (1)
        {
            err = epoll_wait(epfd, &ev_globalfifo, 1, 5000);
            if (err < 0)
            {
                perror("epoll_wait()");
            }
            else if (err == 0)
            {
                printf("no event\n");
            }
            else
            {
                printf("err = %d, events = %d\n", err, ev_globalfifo.events);
                if (ev_globalfifo.events & EPOLLIN)
                {
                    printf("read event happen.\n");
                }
                if (ev_globalfifo.events & EPOLLOUT)
                {
                    printf("write event happen.\n");
                }
            }
        }

        err = epoll_ctl(epfd, EPOLL_CTL_DEL, fd, &ev_globalfifo);
        if (err < 0)
        {
            perror("epoll_ctl()");
        }

        close(fd);

    }
}